require 'rubygems'
require 'open-uri'

class StoreController < ApplicationController
  before_filter :login_required,:only => [:mine,:my_favorite,:buy_history]
  before_filter :find_user,:only=>[:mine,:my_favorite,:buy_history]
  before_filter :find_cart, :except => :empty_cart
  before_filter :build_order_conditions,:only=>[:mine,:print]
  def index
    @newest = Product.all_active(4)
    @recommends = Product.recommendation(8)
  end

  def add_to_cart
    begin
      Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}")
      redirect_to_index("Invalid product")
    else
      quantity = params[:quantity] ? (params[:quantity].to_i):1
      @current_item = @cart.add_product(params[:id],quantity)
      if request.xhr?
        render :text => "{quantity:#{@cart.total_items},total_price:#{@cart.total_price}}"
      else
        redirect_to_index
      end
    end
  end

  def my_cart

  end

  def remove_from_cart
    @cart.items.each do |i|
      @cart.items.delete(i)   if i.product_id == params[:id]
    end
    render :text=>@cart.total_price
  end

  def change_cart_quantity
    if params[:quantity] && params[:id]
      quantity = params[:quantity].to_i
      item_price = 0.1
      @cart.items.each do |i|
        if i.product_id == params[:id]
          i.set_quantity(quantity)
          item_price = i.price
        end
      end
      render :text=> "{item_price:#{item_price},total_price:#{@cart.total_price}}"
    end
  end

  def empty_cart
    session[:cart] = nil
    redirect_to(:action=>:my_cart)
  end

  def mine
    unless admin?
      @orders = @user.orders.paginate :page => params[:page],:per_page =>5,:conditions=>@coditions,:order=>'id asc'
    else
      @orders = Order.paginate :all,:page => params[:page],:per_page =>5,:conditions=>@coditions,:order=>'id asc'
    end
  end

  def my_favorite

  end

  def buy_history

  end

  def print
    if params[:id]
      @orders = Order.find :all,:conditions=>["id = ?",params[:id]]
    else
      @orders = Order.find :all,:conditions=>@coditions,:order=>'id asc'
    end
    render :layout=>false
  end


  def tips
    
  end


  def calculate_fee
    if params[:logistic_id] && params[:address_id]
      logistic = Logistic.find(params[:logistic_id])
      if @cart.total_items >=logistic.free_quantity && logistic.free_quantity >0
        render :text=>"#{@cart.total_items}件免运费"
      else
        province_id =  Address.find(params[:address_id]).province_id
        logistic_fee = LogisticFee.find_by_province_id_and_logistic_id(province_id,logistic.id)

        if logistic_fee
          render :text=>"<span class='yen'>" + logistic_fee.fee.to_s + "</span>"
        else
          render :text=>"<span class='yen'>" + logistic.default_fee.to_s + "</span>"
        end
      end
    else
      render :text=>""
    end
  end

 
  def fqa
    
  end


  private


  def empty_cart_or_not
    session[:cart] = nil if params[:empty_cart]
  end

  def build_order_conditions
    if params[:status] && !params[:start_date].blank? && !params[:end_date].blank?
      start_date = Date.parse(params[:start_date]) - 1.day
      end_date = Date.parse(params[:end_date]) + 1.day
      @coditions = ["status = ? and created_at between ? and ?",params[:status], start_date,end_date]
    elsif params[:status]
      @coditions = ["status = ?",params[:status]]
    else
      @coditions = ""
    end
  end
end
